package com.neulogistics.contrib.procuredcenter.api.application.command.handler;

import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.contrib.procuredcenter.api.application.command.CreateGoodsListCommand;
import com.neulogistics.contrib.procuredcenter.api.application.command.CreateGoodsSetCommand;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.GoodsList;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.GoodsSet;
import com.neulogistics.contrib.procuredcenter.domain.repository.IGoodsListRepository;
import com.neulogistics.contrib.procuredcenter.domain.repository.IGoodsSetRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class CreateGoodsSetCommandHandler implements IRequestHandler<CreateGoodsSetCommand, BaseResponse<String>> {
    @Resource
    IGoodsListRepository goodsListRepository;

    @Resource
    IGoodsSetRepository goodsSetRepository;

    @Override
    public BaseResponse<String> handle(CreateGoodsSetCommand command) throws Exception {
        GoodsList goodsList = goodsListRepository.findById(command.listId);
        if(goodsList == null){
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
        }

        GoodsSet goodsSet = new GoodsSet(command.listId, command.setName, command.setDescription);
        goodsSetRepository.save(goodsSet);

        return ResultUtils.success(goodsSet.getSetId().toString());
    }
}
